from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .models import User, UserProfile, StudentProfile, TeacherProfile


@admin.register(User)
class UserAdmin(BaseUserAdmin):
    list_display = ['username', 'email', 'role', 'real_name', 'is_active', 'created_at']
    list_filter = ['role', 'is_active', 'is_staff', 'created_at']
    search_fields = ['username', 'email', 'real_name', 'phone']
    ordering = ['-created_at']

    fieldsets = BaseUserAdmin.fieldsets + (
        ('额外信息', {
            'fields': ('role', 'real_name', 'phone', 'avatar', 'is_verified')
        }),
    )


@admin.register(StudentProfile)
class StudentProfileAdmin(admin.ModelAdmin):
    list_display = ['user', 'student_id', 'class_name', 'grade', 'gpa']
    list_filter = ['grade', 'class_name']
    search_fields = ['user__username', 'user__real_name', 'student_id']

@admin.register(TeacherProfile)
class TeacherProfileAdmin(admin.ModelAdmin):
    list_display = ['user', 'teacher_id', 'department', 'title']
    list_filter = ['department', 'title']
    search_fields = ['user__username', 'user__real_name', 'teacher_id']

@admin.register(UserProfile)
class UserProfileAdmin(admin.ModelAdmin):
    list_display = ['user', 'bio']
    search_fields = ['user__username', 'user__real_name']

    def user_role(self, obj):
        return obj.user.get_role_display()
    user_role.short_description = '用户角色'